<?php
/**
 * render block of region
 */
class BlockRenderWidget extends CWidget{
    public $page;
    public $region;

    public function run(){
        //doing something
        //read the cache
        $blocks = app()->cache->get('frontend_blocks_cache_page_'.$this->page->page_id.$this->region);
        if(!$blocks) {
            $blocks = PageBlock::model()->findAll(array(
                                    'condition'=>'page_id=:paramId and region='.$this->region.' and status=1',
                                    'params'=>array(':paramId'=>$this->page->page_id)
                                ));

            //set cache
            app()->cache->set('frontend_blocks_cache_page_'.$this->page->page_id.$this->region, $blocks);
        }

        foreach($blocks as $block){
            $model = app()->cache->get('frontend_block_cache_'.$block->block_id);
            if(!$model){
                $model = Block::model()->findByPk($block->block_id);

                //write to cache
                app()->cache->set('frontend_block_cache_'.$block->block_id, $model);
            }

            $iniInfo=parse_ini_file(Yii::getPathOfAlias('common.blocks.'.$model->type).DIRECTORY_SEPARATOR.'info.ini');

            //Include the class
            Yii::import('common.blocks.'.$model->type.'.'.$iniInfo['class']);

            //parse ini
            $this->widget("common.blocks.{$model->type}.{$iniInfo['class']}", array(
                'block' => $model
            ));
        }
    }
}